package fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.boltshow.LoginActivity;
import com.example.administrator.boltshow.R;

import java.util.ArrayList;
import java.util.List;

import adapter.PraiseViewPagerAdapter;
import application.MyApplication;

/**
 * Created by Administrator on 2016/8/29.
 */
public class PraiseFragment extends Fragment {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<String> mTitle;
    private List<Fragment> mFragments;
    private PraiseViewPagerAdapter mAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_priase, null);
        intView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {


        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                int position = tab.getPosition();
                if(position==1){

                    MyApplication mApp= (MyApplication) getActivity().getApplication();
                    if(mApp.getIsLogin()) {
                        String userName = mApp.getUserName();



                    }else {
                        Intent intent=new Intent(getActivity(), LoginActivity.class);
                        getActivity().startActivity(intent);
                    }

                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });



    }

    private void setData() {
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);


    }

    private void initData() {
        mTitle = new ArrayList<>();
        mTitle.add("聚赞");
        mTitle.add("我的聚赞");
        mFragments = new ArrayList<>();
        for (int i = 0; i < mTitle.size(); i++) {
            if (i == 0) {
                mFragments.add(new AllPraiseFragment());
            } else if (i == 1) {
                mFragments.add(new MineAllPraiseFragment());

            }
          mAdapter=new PraiseViewPagerAdapter(getChildFragmentManager(),mFragments,mTitle);

        }


    }

    private void intView(View view) {
        mTabLayout = (TabLayout) view.findViewById(R.id.tb_praise_title);
        mViewPager = (ViewPager) view.findViewById(R.id.vp_praise_show);
     /*   ObjectAnimator.ofFloat(mViewPager, "rotationY",360).setDuration(2000)
                .start();
*/




    }
}
